package com.sunni.controller;

import com.sunni.entity.Menu;
import com.sunni.entity.PageListRes;
import com.sunni.entity.QueryVo;
import com.sunni.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @RequestMapping("/getJspPage")
    public String getJspPage(){
        return "menu";
    }


    /*课时101-菜单管理 显示所有Menu (Menu表)*/
    @RequestMapping("/selectAllMenu")
    @ResponseBody
    public PageListRes selectAllMenu(QueryVo queryVo){
        return menuService.selectAllMenu(queryVo);
    }

    /*课时102-新增按钮,dialog form表单的 ‘父菜单’的下拉框-获取所有menu*/
    @RequestMapping("/selectAllMenuNoPage")
    @ResponseBody
    public List<Menu> parentMenu(){
        List<Menu> menuList = menuService.selectAllMenuNoPage();
        menuList.forEach(menu -> System.out.println("menu = " + menu));
        return menuList;
    }


    /*课时103-菜单管理页 添加按钮 insert进数据库menu表*/
    @RequestMapping("/addMenu")
    @ResponseBody
    public AjaxRes addMenu(Menu menu){
        AjaxRes ajaxRes = new AjaxRes(false, "添加失败");


        try {
            menuService.addMenu(menu);
            ajaxRes.setSuccess(true);
            ajaxRes.setMsg("添加成功");
        } catch (Exception e) {
            System.out.println("e = " + e);
        }

        return ajaxRes;
    }

    /*/*课时105：菜单管理页，修改按钮，修改(Service层业务逻辑：不能选择子菜单做父菜单，那就判断我选择的父菜单的父菜单是不是我自己)*/
    @RequestMapping("/updateMenu")
    @ResponseBody
    public AjaxRes updateMenu(Menu menu){
        return menuService.updateMenu(menu);
    }


    /*课时106：菜单管理页，删除按钮，删除menu.（业务逻辑，删除menu时判断，有没有子menu，有的话提示不能删除）*/
    @RequestMapping("/deleteMenu")
    @ResponseBody
    public AjaxRes deleteMenu(Long id){
        return menuService.deleteMenu(id);
    }

    /*课时107- 查询所有一级menu及其子menu*/
    @RequestMapping("/selectTreeMenu")
    @ResponseBody
    public List<Menu> selectTreeMenu(){
        return menuService.selectTreeMenu();
    }
}
